package com.ruoyi.web.cp.service.rm;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.ruoyi.common.utils.JsVarParser;
import com.ruoyi.web.core.config.ConfigLoader;
import com.ruoyi.web.cp.domain.rm.RmChatShow;
import com.ruoyi.web.cp.service.http.RmHttpService;
import com.ruoyi.web.mapper.RmChatShowMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xiesm
 * @Description: TODO
 * @date 2025-07-30 13:40
 */
@Slf4j
@Service
public class RmChatShowService {
    @Autowired
    private RmHttpService rmHttpService;
    @Autowired
    private ConfigLoader configLoader;

    public void getRmChatShow() {
        String path = configLoader.getRmPath() + "rm-chatShow.txt";
        if (!FileUtil.exist(path)) {
            return;
        }
        List<String> games = FileUtil.readLines(path, "utf8");
        if (CollUtil.isEmpty(games)) {
            return;
        }
        String path2 = configLoader.getRmPath() + "rm-chatShow-details.txt";
        FileUtil.writeString("", path2, "utf8");
        FileUtil.clean(path2);
        RmChatShowService service = SpringUtil.getBean("rmChatShowService", RmChatShowService.class);
        for (String game : games) {
            List<String> lists = new ArrayList<>();
            lists.add(game);
            String[] strs = game.split("\t");
            String res = rmHttpService.readRmCharShow(strs[0].trim(), strs[1].trim());
            lists.addAll(JsVarParser.parseChatShow(res));
            FileUtil.writeLines(lists, path2, "utf8", true);
            service.saveRmChatShow(lists);
        }
    }

    @Async
    public void saveRmChatShow(List<String> games) {
        if (CollUtil.isEmpty(games)) {
            return;
        }
        try {
            RmChatShowService service = SpringUtil.getBean("rmChatShowService", RmChatShowService.class);
            RmChatShow enity = null;
            for (int i = 0; i < games.size(); i++) {
                String str = games.get(i);
                if (str.contains("2025-")) {
                    String[] strs = str.split("\t");
                    service.saveOrUpdateByGameId(enity);
                    enity = new RmChatShow(strs[0], strs[1]);
                } else {
                    String[] strs = str.split("\t");
                    RmChatShow.Detail detail = new RmChatShow.Detail(strs[0], strs[1]);
                    enity.getDetails().add(detail);
                }
                if (i == (games.size() - 1)) {
                    service.saveOrUpdateByGameId(enity);
                }
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

    @Transactional
    public void saveOrUpdateByGameId(RmChatShow entity) {
        if (entity == null) {
            return;
        }
        RmChatShowMapper mapper = SpringUtil.getBean("rmChatShowMapper", RmChatShowMapper.class);
        // 1. 查询是否已存在记录
        List<RmChatShow> existing = mapper.selectByGameId(entity.getGameId());
        if (CollUtil.isEmpty(existing)) {
            mapper.insertRmChatShow(entity);
        } else {
            mapper.updateRmChatShow(entity.getDetailsJson(), entity.getGameId());
        }
    }

}
